
pub mod path {
    use std::env;
    use std::fs;
    use std::io::Result;
    use std::path::PathBuf;

    #[cfg(target_os = "macos")]
    pub fn get_path() -> Result<PathBuf> {
        let mut dir = env::current_exe()?;
        dir.pop();
        dir.pop();
        dir.pop();
        dir.pop();
        Ok(dir)
    }

    #[cfg(not(target_os = "macos"))]
    pub fn get_path() -> Result<PathBuf> {
        let mut dir = env::current_exe()?;
        dir.pop();
        Ok(dir)
    }

    /**
     * 创建任务文件夹模板
     */
    pub fn create_folder(x: i32, name: &str) -> Result<()> {
        let dir: PathBuf = get_path().unwrap();
        let folder = dir.join(x.to_string() + "_" + name);
        println!("{:?}{:?}", dir, folder.join("0-来文"));
        fs::create_dir_all(folder.join("0-来文"))?;
        fs::create_dir_all(folder.join("1-草稿"))?;
        Ok(())
    }
}

pub mod num {

    use std::fs;
    use std::path::PathBuf;

    use super::path::get_path;
    /**
     * 读取 foo 文件获取最新数字，如果未获取到则返回 1
     */
    pub fn get_number(filename: &str) -> i32 {
        let dir: PathBuf = get_path().unwrap();
        match fs::read_to_string(dir.join(filename)) {
            Ok(v) => v.trim().parse().unwrap_or(0) + 1,
            Err(_) => 1,
        }
    }

    /**
     * 将当前数字存储到 foo 文件
     */
    pub fn save_number(filename: &str, x: i32) {
        let dir: PathBuf = get_path().unwrap();
        match fs::write(dir.join(filename), x.to_string()) {
            Ok(_) => (),
            Err(e) => println!("发生错误 {}", e),
        }
    }
}
